package com.yeren.ams.provider.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.yeren.ams.api.model.Account;
import com.yeren.ams.api.utils.PageBean;
import com.yeren.ams.provider.dao.AccountDao;

@Repository
public class AccountDaoImpl implements AccountDao {
	@Autowired
	@Qualifier("sessionFactory")
	SessionFactory sf;

	@Override
	public void save(Account account) {
		Session session = sf.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.save(account);
			tx.commit();
		} catch (RuntimeException e) {
			tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}

	}

	@Override
	public int delete(Integer id) {
		Session session = sf.openSession();
		Transaction tx = null;
		int executeUpdate = 0;
		try {
			tx = session.beginTransaction();
			String hql = "delete from Account where id=:id";
			org.hibernate.Query query = session.createQuery(hql);
			query.setInteger("id", id);
			executeUpdate = query.executeUpdate();
			tx.commit();
		} catch (RuntimeException e) {
			tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return executeUpdate;
	}

	@Override
	public void update(Account account) {
		Session session = sf.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(account);
			tx.commit();
		} catch (RuntimeException e) {
			tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Account> findById(Integer id) {
		Session session = sf.openSession();
		List<Account> list = null;
		try {
			String hql = "from Account where id=:id";
			org.hibernate.Query query = session.createQuery(hql);
			query.setInteger("id", id);
			list = query.list();
		} catch (RuntimeException e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return list;
	}

	// 模糊查询（Hibernate）
	@SuppressWarnings("unchecked")
	@Override
	public List<Account> findByCondition(String condition) {
		Session session = sf.openSession();
		Criteria criteria = session.createCriteria(Account.class);
		Criterion conditionAccount = Restrictions.like("account", "%" + condition + "%");
		Criterion conditionName = Restrictions.like("name", "%" + condition + "%");
		Criterion conditionSex = Restrictions.like("sex", "%" + condition + "%");
		criteria.add(Restrictions.or(conditionAccount, conditionName, conditionSex));
		return criteria.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Account> findAll(PageBean pb) {

		Session session = sf.openSession();
		List<Account> list = null;
		try {
			String hql = "from Account";
			org.hibernate.Query query = session.createQuery(hql);
			query.setFirstResult(pb.getStart());
			query.setMaxResults(pb.getRows());
			list = query.list();
		} catch (RuntimeException e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean isExitAccount(String account) {
		Session session = sf.openSession();
		try {
			String hql = "from Account where account=:account";
			org.hibernate.Query query = session.createQuery(hql);
			query.setString("account", account);
			List<Account> list = query.list();
			if (list.size() >= 1) {
				return true;
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return false;
	}

	@Override
	public int modifyPassword(Account account) {
		Session session = sf.openSession();
		Transaction tx = null;
		int executeUpdate = 0;
		try {
			tx = session.beginTransaction();
			String hql = "update Account set password=:password where id=:id";
			org.hibernate.Query query = session.createQuery(hql);
			query.setInteger("id", account.getId());
			query.setString("password", account.getPassword());
			executeUpdate = query.executeUpdate();
			tx.commit();
		} catch (RuntimeException e) {
			tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return executeUpdate;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Account login(Account account) {
		Session session = sf.openSession();
		List<Account> list = null;
		try {
			String hql = "from Account where account=:account and password=:password";
			org.hibernate.Query query = session.createQuery(hql);
			query.setString("account", account.getAccount());
			query.setString("password", account.getPassword());
			list = query.list();

		} catch (RuntimeException e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		if (list.size() == 0) {
			return null;
		} else {
			return list.get(0);
		}
	}

}
